Java编程题-武器数组作业

写一个类 Army,代表一支军队,这个类有一个属性 Weapon 数组 w (用来存储该军队所拥有的所有的武器),该类还提供一个构造方法,在构造方法里通过传一个 int 类型的参数来限定该类所能拥有的最大武器数量,并用这一大小来初始化数组 w。

该类还提供一个方法 addWeapon(Weapon wa),表示把参数 wa 所代表的武器加入到数组 w 中。在这个类中还定义两个方法 attackAll() 让 w 数组中的所有武器攻击;以及 moveAll() 让 w 数组中的所有可移动的武器移动。

写一个主方法去测试以上程序。

提示:Weapon 是一个父类,应该有很多子武器。这些子武器应该有一些是可移动的,有一些是可攻击的。

接口

Moveable 可移动的接口

1
2
3
4
5
6
public interface Moveable {
/**
* 移动行为
*/
void move();
}

Shootable 可攻击的接口

1
2
3
4
5
6
public interface Shootable {
/**
* 射击行为
*/
void shoot();
}

Weapon 父类

1
2
3
4
5
/**
* 所有武器的父类
*/
public class Weapon {
}

子类

Fighter 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 飞机是武器,可以移动,可以攻击
*/
public class Fighter extends Weapon implements Moveable, Shootable {
@Override
public void shoot() {
// TODO Auto-generated method stub
System.out.println("飞机射击!");
}

@Override
public void move() {
// TODO Auto-generated method stub
System.out.println("飞机移动!");
}
}

GaoShePao 类

1
2
3
4
5
6
7
8
9
10
/**
* 高射炮是武器,但不能移动,可射击。
*/
public class GaoShePao extends Weapon implements Shootable {
@Override
public void shoot() {
// TODO Auto-generated method stub
System.out.println("高射炮放炮!");
}
}

Tank 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 坦克是武器,可移动,可射击。
*/
public class Tank extends Weapon implements Moveable, Shootable {
@Override
public void shoot() {
// TODO Auto-generated method stub
System.out.println("坦克放炮!");
}

@Override
public void move() {
// TODO Auto-generated method stub
System.out.println("坦克移动!");
}
}

WuZiFeiJi 类

1
2
3
4
5
6
7
8
9
10
11
/**
* 物资运输飞机,是武器,但只能移动,不能攻击。
*/
public class WuZiFeiJi extends Weapon implements Moveable {

@Override
public void move() {
// TODO Auto-generated method stub
System.out.println("物资飞机移动!");
}
}

Army 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 军队类
*/
public class Army {
/**
* 武器数组
*/
private Weapon[] weapons;

/**
* @Description: 创建军队的构造方法
* @param count:武器
*/
public Army(int count) {
// 动态初始化数组中每一个元素默认值是 null
weapons = new Weapon[count];
}

/**
* @Description: 将武器加入数组
* @param wa
* @throws AddWeaponException
*/
public void addWeapon(Weapon wa) throws AddWeaponException {
for (int i = 0; i < weapons.length; i++) {
if (weapons[i] == null) {
weapons[i] = wa;
return;
}
}
// 程序如果执行到此处说明:武器没有添加成功
throw new AddWeaponException("武器添加已达上限!");
}

/**
* @Description: 所有可攻击的武器攻击
*/
public void attackAll() {
for (int i = 0; i < weapons.length; i++) {
if (weapons[i] instanceof Shootable) {
Shootable shootable = (Shootable) weapons[i];
shootable.shoot();
}
}
}

/**
* @Description: 所有可移动的武器移动
*/
public void moveAll() {
for (int i = 0; i < weapons.length; i++) {
if (weapons[i] instanceof Moveable) {
Moveable moveable = (Moveable) weapons[i];
moveable.move();
}
}
}
}

AddWeaponException 异常类

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 添加武器异常
*/
public class AddWeaponException extends Exception {
public AddWeaponException() {

}

public AddWeaponException(String s) {
super(s);
}
}

Test 测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Test {

public static void main(String[] args) throws AddWeaponException {
// 构建一个军队
Army army = new Army(4);

// 创建武器对象
Fighter fighter = new Fighter();
Fighter fighter2 = new Fighter();
Tank tank = new Tank();
WuZiFeiJi wuZiFeiJi = new WuZiFeiJi();
GaoShePao gaoShePao = new GaoShePao();

// 添加武器
try {
army.addWeapon(fighter);
army.addWeapon(tank);
army.addWeapon(wuZiFeiJi);
army.addWeapon(gaoShePao);
army.addWeapon(fighter2);
} catch (Exception e) {
System.out.println(e.getMessage());
}

// 让所有可移动的移动
army.attackAll();

// 让所有可攻击的攻击
army.moveAll();
}
}